package mq.rabbitmq;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * 生产者
 *
 * @author verlet
 * @date 2019/9/11
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 声明channel
        Channel channel = RabbitMqChannelFactory.createChannel();
        // 消息属性
        Map<String, Object> map = new HashMap<>(2);
        map.put("headers1", "value1");
        map.put("headers2", "value2");
        AMQP.BasicProperties properties = new AMQP.BasicProperties().builder()
                // 2时表示该消息需要被持久化支持
                .deliveryMode(2)
                // 标明消息的编码
                .contentEncoding("UTF-8")
                // 消息有效期
                .expiration("20000")
                .headers(map)
                .build();

        // 发布消息
        channel.basicPublish("", "test1001", properties, "Hello RabbitMQ!".getBytes());
        System.out.println("发布一条消息成功。");
        // 关闭连接
        RabbitMqChannelFactory.close(channel);
    }
}